package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.RecordAdapter;
import com.wnhz.work.yz.bean.RecordsItemBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class OtherRecordSearchActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_record_search);
        initData();
        initView();
    }

    private String type = "";
    private EditText et_search;
    private String strHint = "";
    private RecyclerView search_recycler;

    private void initData() {
        type = getIntent().getStringExtra("type");
        //  Toast.makeText(OtherSearchActivity.this, ""+type, Toast.LENGTH_SHORT).show();
        strHint = getIntent().getStringExtra("searchStrHint");
    }


    private void initView() {
        et_search = (EditText) findViewById(R.id.et_search);
        et_search.setHint(strHint);
        search_recycler = (RecyclerView) findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        search_recycler.setLayoutManager(linearLayoutManager);

        findViewById(R.id.cancel).setOnClickListener(this);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    upSearch();
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
        }
    }

    List<RecordsItemBean.InfoEntity> infos = new ArrayList<>();
    private RecordAdapter adapter;

    private void upSearch() {

        Map<String, Object> map = new Hashtable<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        map.put("keywords", et_search.getText().toString());
        XUtil.Post(Url.API_RECORDLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result);
                Gson gson = new Gson();
                RecordsItemBean b = gson.fromJson(result, RecordsItemBean.class);
                if (b.getResult().equals("1")) {
                    infos = b.getInfo();
                }
            }

            @Override
            public void onFinished() {
                setData();
                super.onFinished();
            }


        });
    }

    private void setData() {
        adapter = new RecordAdapter(this, infos);
        search_recycler.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
